package com.ericcson.innerportalapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class EmergencyActivity extends FragmentActivity {

	private Map<String, ArrayList<Emergency>> map = new HashMap<String, ArrayList<Emergency>>();
	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	private SectionsPagerAdapter mSectionsPagerAdapter;
	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	private ViewPager mViewPager;
	private ArrayList<ArrayList<String>> fromjsonlist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_emergency);
		map = new HashMap<String, ArrayList<Emergency>>();

		String sss = "[{\"Security\":[{\"Person_Name\":\"Ericsson Emergency Support\",\"Person_Number\":\"+468241010\"},{\"Person_Name\":\"Regional Security Director\",\"Person_Number\":\"+971556001136\"},{\"Person_Name\":\"CU NEA Security Manager\",\"Person_Number\":\"+201000059888\"}]},{\"Travel - Hotel & Flight\":[{\"Person_Name\":\"Vista Tourism Emergency\",\"Person_Number\":\"+905320000000\"},{\"Person_Name\":\"Turkish Airlines\",\"Person_Number\":\"+908503330849\"},{\"Person_Name\":\"AtlasJet\",\"Person_Number\":\"+908502220000\"}]},{\"Office Related\":[{\"Person_Name\":\"Onder Savas\",\"Person_Number\":\"+905322974810\"},{\"Person_Name\":\"Adnan Pekdemir\",\"Person_Number\":\"+905327210780\"},{\"Person_Name\":\"Farzaneh Tabrizi\",\"Person_Number\":\"+905336342338\"}]},{\"Local\":[{\"Person_Name\":\"Ambulance\",\"Person_Number\":\"112\"},{\"Person_Name\":\"Fire Department\",\"Person_Number\":\"110\"},{\"Person_Name\":\"Police Department\",\"Person_Number\":\"155\"}]}]";
		sss.replace("\"", "");
		try {
			JSONArray acb = new JSONArray(sss);
			fromjsonlist = parseJson(acb);
		} catch (JSONException e) {
		}

		for (int i = 0; i < fromjsonlist.size(); i++) {
			ArrayList<String> badlist = new ArrayList<String>();
			badlist = fromjsonlist.get(i);
			ArrayList<Emergency> jsoncolon = new ArrayList<Emergency>();

			for (int iz = 1; iz + 1 < badlist.size(); iz += 2) {
				String name = badlist.get(iz).toString();
				String number = badlist.get(iz + 1).toString();
				jsoncolon.add(new Emergency(name, number));
			}
			String titleName = fromjsonlist.get(i).get(0);
			map.put(titleName, jsoncolon);

		}

		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		ActionBar ab = getActionBar();
		ab.setDisplayHomeAsUpEnabled(true);
		ab.setTitle("Emergency");
		ab.setIcon(R.drawable.ic_action_warning);
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.emergency, menu);
//		return true;
//	}

	public ArrayList<ArrayList<String>> parseJson(JSONArray jary) {

		ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
		try {
			for (int i = 0; i < jary.length(); i++) {
				JSONObject job = jary.getJSONObject(i);
				String abc = job.keys().next().toString();
				ArrayList<String> listt = new ArrayList<String>();
				listt.add(abc);
				JSONArray arr = job.getJSONArray(abc);
				for (int iz = 0; iz < arr.length(); iz++) {
					JSONObject job2 = arr.getJSONObject(iz);
					String name = job2.getString("Person_Name");
					listt.add(name);
					String number = job2.getString("Person_Number");
					listt.add(number);
				}
				list.add(new ArrayList<String>(listt));
				listt.clear();
			}

		} catch (JSONException e) {
		}
		return list;
	}

	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		private ArrayList<String> names;
		private ArrayList<ArrayList<Emergency>> values;

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
			names = new ArrayList<String>(map.keySet());
			values = new ArrayList<ArrayList<Emergency>>(map.values());
		}

		@Override
		public Fragment getItem(int position) {

			Fragment emergencyFragment = new EmergencyFragment();
			Bundle bundle = new Bundle();

			bundle.putInt(EmergencyFragment.ARG_SECTION_NUMBER, position);
			bundle.putSerializable("namesArray", names);
			bundle.putSerializable("valuesArray", values);
			emergencyFragment.setArguments(bundle);
			return emergencyFragment;
		}

		@Override
		public int getCount() {
			return names.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {

			for (int i = 0; i < names.size(); i++) {
				if (i == position) {
					return names.get(i);
				}
			}
			return null;
		}
	}
}
